package com.chahu.service.impl;

import java.util.Collections;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;

import com.chahu.entity.Order;
import com.chahu.mapper.OrderMapper;
import com.chahu.service.OrderService;
import com.chahu.utils.MybatisUtil;

public class OrderServiceImpl implements OrderService {

	@Override
	public List<Order> page(int page, int pageSize) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
			OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            List<Order> list = mapper.page(page, pageSize);
            if(list == null){
                return Collections.emptyList();
            }
            return list;
        }
	}

	@Override
	public boolean deleteById(int id) {
		try (SqlSession sqlSession = MybatisUtil.getFactory().openSession(true)) {
			OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
            try {
            	mapper.deleteById(id);
            } catch (PersistenceException e) {
                System.out.println("删除失败，异常信息：" + e.getMessage());
                return false;
            }
        }
		return true;
	}
    
}
